﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ZLCMS.Data.Model;
using Newtonsoft.Json.Linq;
using ZLCMS.Business.Config;
using ZLCMS.Business.Tree;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.TypeTree
{
    public partial class TypeTree : System.Web.UI.Page
    {
        string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin(); 
            //判断操作类型
            switch(Request["opt"])
            {
                case "add":
                    AddCallBack(); break;

                case "edit":
                    EditCallBack(); break;

                case "del":
                    DeleteCallBack(); break;
            }
        }

        #region 获取节点json对象 -protected ZTreeDataType GetRequestNodeData()
        /// <summary>
        /// 获取节点json对象
        /// </summary>
        /// <returns></returns>
        protected ZTreeDataType GetRequestNodeData()
        {
            string jsonNode = Request["node"];

            return JsonConvert.DeserializeObject<ZTreeDataType>(jsonNode);
        } 
        #endregion

        #region 新增 -protected void AddCallBack()
        /// <summary>
        /// 新增
        /// </summary>
        protected void AddCallBack()
        {
            ZTreeDataType parentData = GetRequestNodeData();

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                CmsTreeDef parent = session.Get<CmsTreeDef>(parentData.Id);

                CmsTreeDef node = new CmsTreeDef();

                node.Parent = parent.Id;
                node.Name = "新增节点";
                node.Kind = parent.Kind;
                node.OwnerAccount = _userAccount;

                session.Save(node); 
                node.Prefix = parent.Prefix + node.Id + "_";

                session.Flush();

                Response.Write(JsonConvert.SerializeObject(node));
                Response.End();
            }
        } 
        #endregion

        #region 编辑 -protected void EditCallBack()
        /// <summary>
        /// 编辑
        /// </summary>
        protected void EditCallBack()
        {
            ZTreeDataType nodeData = GetRequestNodeData();
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                CmsTreeDef node = session.Get<CmsTreeDef>(nodeData.Id);

                node.Name = nodeData.Name;

                session.Update(node);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(node));
                Response.End();
            }
        } 
        #endregion

        #region 删除 -protected void DeleteCallBack()
        /// <summary>
        /// 删除
        /// </summary>
        protected void DeleteCallBack()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                CmsTreeDef node = session.Get<CmsTreeDef>(GetRequestNodeData().Id);

                IList<CmsTreeDef> list = session.CreateCriteria<CmsTreeDef>().SetMaxResults(10000).List<CmsTreeDef>();

                List<int> ids = new List<int>();

                GetChildNodeIds(list, node.Id, ref ids);

                foreach (int index in ids)
                {
                    foreach (CmsTreeDef type in list)
                    {
                        if (type.Id == index)
                        {
                            session.Delete(type);
                        }
                    }
                }
                session.Flush();
                Response.Write(JsonConvert.SerializeObject(node));
                Response.End();
            }
        } 
        #endregion

        #region 获取子节点ID - public void GetChildNodeIds(IList<CmsTreeDef> list, int id, ref List<int> ids)
        /// <summary>
        /// 获取子节点ID
        /// </summary>
        /// <param name="list"></param>
        /// <param name="id"></param>
        /// <param name="ids"></param>
        public void GetChildNodeIds(IList<CmsTreeDef> list, int id, ref List<int> ids)
        {
            foreach (CmsTreeDef type in list)
            {
                if (type.Id == id)
                {
                    {
                        ids.Add(type.Id);
                    }

                }
                if (type.Parent == id)
                {
                    GetChildNodeIds(list, type.Id, ref ids);
                }
            }
        } 
        #endregion
    }
}
